﻿/// <reference path="../vsdoc/jquery-1.8.3-vsdoc.js" />

/*
** some Table enhancements
*/

/* ListView */

/* Delete table row */
$(document).ready(function ()
{
    /* AssureDelete */
    // assure dialogue
    $("TD A.assureDelete").live('click', function (e)
    {
        e.preventDefault();
        var href = $(this).attr("href");
        var result = confirm($(this).data("assuremessage"));
        if (result)
        {
            var form = $("#doDeleteForm") // fix IE7 and IE8 async load of a FORM element
            var input = $("INPUT[name=__RequestVerificationToken]", form);
            if (input.length == 0)
            {
                var input = $(form).next("INPUT[name=__RequestVerificationToken]");
                if (input.length != 0)
                {
                    form.append(input);
                }
            }

            // converts an anchor delete action into the form submit
            form.attr("action", href)
            .attr("method", "POST")
            .submit();
        }
        return false;
    });
});

/* ListView */
/* GetFocus to ChildWindow when Target is not self */
var childWindow;
function GetFocusToChild()
{
    try
    {
        if (childWindow != null) { childWindow.focus(); }
    } catch (ex) { }
}
window.onload = function ()
{
    try
    {
        if (window.opener != null) { window.opener.childWindow = window; }
    } catch (ex) { }
};

/* LIST checkobxes */
$(document).ready(function ()
{
    $("INPUT.selectAllListed").live('click', function (e)
    {
        var checked = $(this).attr("checked");
        $("INPUT:checkbox", $(this).parents("TABLE")).each(function (e)
        {
            $(this).attr("checked", checked);
        });
    });
});

/* Mark the selected row with different style */
var lastClicked;
function TrClickHandler()
{ // this is reused in the AsyncListView
    $("TABLE TR").live('click', function ()
    {
        if (lastClicked != null)
        {
            lastClicked.removeClass("marked");
        }
        $(this).addClass("marked");
        lastClicked = $(this);
    });
    $("A[target]").live('click', function () { GetFocusToChild() });
}

$(document).ready(TrClickHandler);

